<?php
require_once "INews.php";
class PointCut{
    const BEFORE = 'before';
    const AFTER = 'after';
}
/**
 * Class MyProxy
 * @method editNews(int $newId)
 * @method delNews
 */
class MyProxy
{
    private $myObject;
    private $beforeList=[];
    private $afterList=[];
    public function addPoint(callable $func, $point)
    {
        if ($point == PointCut::BEFORE){
            $this->beforeList[] = $func;
        }
        if ($point == PointCut::AFTER){
            $this->afterList[] = $func;
        }
    }

    /**
     * 构造函数
     * @param $obj
     */
    function __construct($obj)
    {
        $this->myObject = $obj;
    }
    public function __call($name, $arguments)
    {
        $c = new ReflectionClass($this->myObject);
        if ($c->hasMethod($name)){
            $method = $c->getMethod($name);
            $this->preInvoke();
            $res = $method->invoke($this->myObject,...$arguments);
            $this->afterInvoke();
        }
        return false;
    }

    private function preInvoke(){
        foreach ($this->beforeList as $p){
            $p();
        }
    }
    private function afterInvoke()
    {
        foreach ($this->afterList as $p){
            $p();
        }
    }
}